<?php

/**
 * 余额和抱抱币明细接口
 */
class Action
{

    private $per_page = 50;

    private $token = '';
    private $user_id = 0;
    private $user_info = array();
    private $api_stat_id = 0;

    public function __construct() {
        $this->token = an_request('token');

        if ($this->token) {
            $user_info = user_helper::get_user_info_by_token($this->token);
            if ($user_info && $user_info['status'] == 1) {
                $this->user_id = $user_info['id'];
                $this->user_info = $user_info;
            }
        }
    }

    /**
     * 抱抱币明细列表
     * @author xiaolin
     * @date   2016-09-19
     * @return json
     */
    public function bbcoin_record() {
        $page_no = an_request('page_no', 1);
        $store_id = an_request('store_id');
        $time_condition = an_request('time_condition');

        //加密参数
        $sign_param = array(
            'page_no' => $page_no
        );
        if ($store_id) {
            $sign_param = array_merge($sign_param, array('store_id' => $store_id, 'time_condition' => $time_condition));
        }
        /*如果合伙人端要加上时间筛选的话,上面那段代码改成如下代码:(因为time_condition条件是变成必传了)
        $sign_param['time_condition'] = $time_condition;
        if ($store_id) {
            $sign_param['store_id'] = $store_id;
        }
        */

        // 验证接口
        $this->api_stat_id = api_helper::check_sign($sign_param, 1);

        $user_id = -1;
        if ($store_id) {//客户端,这里之所以用store_id,是因为店长也可以查看,但查看的是分店的记录
            $store = store_helper::get_store($store_id);
            $user_id = $store['user_id'];

            if ($this->user_id != $store['user_id'] && $this->user_id != $store['owner_id']) {
                api_helper::return_data(1, '你没有操作权限');
            }
        } else {//合伙人端,如果合伙人端也传一个店铺id过来的话,这个判断就可以去掉了,直接用store_id获取user_id
            $user_id = $this->user_id;
        }
        $filter['user_id'] = $user_id;

        if ($time_condition) {
            switch ($time_condition) {
                case 1:
                    $filter['add_time >='] = datetime(strtotime('now -1 month'), 'Y-m-d 00:00:00');
                    $filter['add_time <='] = datetime();
                    break;
                case 2:
                    $filter['add_time >='] = datetime(strtotime('now -3 month'), 'Y-m-d 00:00:00');
                    $filter['add_time <='] = datetime();
                    break;
                case 3:
                    $filter['add_time >='] = datetime(strtotime('now -6 month'), 'Y-m-d 00:00:00');
                    $filter['add_time <='] = datetime();
                    break;
                case 4:
                    $filter['add_time <='] = datetime(strtotime('now -6 month'), 'Y-m-d 00:00:00');
                    break;
                default:
            }
        }

        $count = _model('bbcoin_record')->getTotal($filter);
        if (!$count) {
            api_helper::return_data(1, '暂无数据');
        }

        //分页配置
        $pager = new Pager($this->per_page);
        $pager->generate($count);
        $page_arr['page_no'] = $page_no;
        $page_arr['pages'] = $pager->getPages();

        $bbcoin_list = _model('bbcoin_record')->field('id', 'bbcoin', 'res_id', 'add_time')->getList($filter, 'ORDER BY `add_time` DESC ' . $pager->getLimit($page_no));

        if ($bbcoin_list) {
            //抱抱币类型
            foreach ($bbcoin_list as $k => $v) {
                $bbcoin_list[$k]['res_name'] = $this->get_record_type_name('bbcoin_type', $v['res_id']);
                unset($bbcoin_list[$k]['res_id']);
            }
        }

        $bbcoin = user_helper::get_bbcoin_by_id($user_id); //获取抱抱币
        api_helper::return_data(0, 'success', array(
            'bbcoin_list' => $bbcoin_list, 
            'bbcoin' => $bbcoin, 
            'pages' => $page_arr
        ), $this->api_stat_id);
    }

    /**
     * 抱抱币记录详情
     * @author xiaolin
     * @date   2016-09-19
     * @return json
     */
    public function bbcoin_record_detail() {
        $bbcoin_record_id = an_request('bbcoin_record_id');

        //参数验证
        $this->id_validation($bbcoin_record_id, '抱抱币记录ID不能为空');

        //加密参数
        $sign_param = array(
            'bbcoin_record_id' => $bbcoin_record_id
        );

        // 验证接口
        $this->api_stat_id = api_helper::check_sign($sign_param, 1);

        $bbcoin_record = _model('bbcoin_record')->field('id', 'bbcoin', 'res_id', 'order_id', 'add_time', 'extra')->read($bbcoin_record_id);
        if (!$bbcoin_record) {
            api_helper::return_data(1, '此抱抱币记录不存在');
        }

        //其他数据获取
        $bbcoin_record['res_name'] = $this->get_record_type_name('bbcoin_type', $bbcoin_record['res_id']);
        $bbcoin_record['order_code'] = $bbcoin_record['order_id'] ? order_helper::order_id_encode($bbcoin_record['order_id']): '';
        unset($bbcoin_record['res_id']);

        api_helper::return_data(0, 'success', array('bbcoin_record' => $bbcoin_record), $this->api_stat_id);
    }

    /**
     * 余额明细列表
     * @author xiaolin
     * @date   2016-09-19
     * @return json
     */
    public function balance_record() {
        $page_no = an_request('page_no', 1);
        $store_id = an_request('store_id');
        $time_condition = an_request('time_condition');

        //加密参数
        $sign_param = array(
            'page_no' => $page_no
        );
        if ($store_id) {
            $sign_param = array_merge($sign_param, array('store_id' => $store_id, 'time_condition' => $time_condition));
        }
        /*如果合伙人端要加上时间筛选的话,上面那段代码改成如下代码:(因为time_condition条件是变成必传了)
        $sign_param['time_condition'] = $time_condition;
        if ($store_id) {
            $sign_param['store_id'] = $store_id;
        }
        */

        // 验证接口
        $this->api_stat_id = api_helper::check_sign($sign_param, 1);

        $user_id = -1;
        if ($store_id) {//客户端,这里之所以用store_id,是因为店长也可以查看,但查看的是分店的记录
            $store = store_helper::get_store($store_id);
            $user_id = $store['user_id'];

            if ($this->user_id != $store['user_id'] && $this->user_id != $store['owner_id']) {
                api_helper::return_data(1, '你没有操作权限');
            }
        } else {//合伙人端,如果合伙人端也传一个店铺id过来的话,这个判断就可以去掉了,直接用store_id获取user_id
            $user_id = $this->user_id;
        }
        $filter['user_id'] = $user_id;

        if ($time_condition) {
            switch ($time_condition) {
                case 1:
                    $filter['add_time >='] = datetime(strtotime('now -1 month'), 'Y-m-d 00:00:00');
                    $filter['add_time <='] = datetime();
                    break;
                case 2:
                    $filter['add_time >='] = datetime(strtotime('now -3 month'), 'Y-m-d 00:00:00');
                    $filter['add_time <='] = datetime();
                    break;
                case 3:
                    $filter['add_time >='] = datetime(strtotime('now -6 month'), 'Y-m-d 00:00:00');
                    $filter['add_time <='] = datetime();
                    break;
                case 4:
                    $filter['add_time <='] = datetime(strtotime('now -6 month'), 'Y-m-d 00:00:00');
                    break;
                default:
            }
        }

        $count = _model('user_money_record')->getTotal($filter);
        if (!$count) {
            api_helper::return_data(1, '暂无数据');
        }

        //分页配置
        $pager = new Pager($this->per_page);
        $pager->generate($count);
        $page_arr['page_no'] = $page_no;
        $page_arr['pages'] = $pager->getPages();

        $user_money_list = _model('user_money_record')->field('id', 'user_money', 'res_id', 'add_time')->getList($filter, 'ORDER BY `add_time` DESC ' . $pager->getLimit($page_no));

        if ($user_money_list) {
            foreach ($user_money_list as $k => $v) {
                $user_money_list[$k]['res_name'] = $this->get_record_type_name('user_money_type', $v['res_id']);
                $user_money_list[$k]['user_money'] = format_money($v['user_money']);
                unset($user_money_list[$k]['res_id']);
            }
        }

        $user_money = user_helper::get_user_money_by_id($user_id); //获取余额
        api_helper::return_data(0, 'success', array(
            'user_money_list' => $user_money_list,
            'user_money' => format_money($user_money),
            'pages' => $page_arr
        ), $this->api_stat_id);
    }

    /**
     * 余额记录详情
     * @author xiaolin
     * @date   2016-09-19
     * @return json
     */
    public function balance_record_detail() {
        $balance_record_id = an_request('balance_record_id');

        //参数验证
        $this->id_validation($balance_record_id, '余额记录ID不能为空');

        //加密参数
        $sign_param = array(
            'balance_record_id' => $balance_record_id
        );

        // 验证接口
        $this->api_stat_id = api_helper::check_sign($sign_param, 1);

        $balance_record = _model('user_money_record')->field('id', 'user_money', 'res_id', 'order_id', 'add_time', 'extra')->read($balance_record_id);
        if (!$balance_record) {
            api_helper::return_data(1, '此余额记录不存在');
        }

        //其他数据获取
        $balance_record['res_name'] = $this->get_record_type_name('user_money_type', $balance_record['res_id']);
        $balance_record['user_money'] = format_money($balance_record['user_money']);
        $balance_record['order_code'] = $balance_record['order_id'] ? order_helper::order_id_encode($balance_record['order_id']): '';
        unset($balance_record['res_id']);

        api_helper::return_data(0, 'success', array('balance_record' => $balance_record), $this->api_stat_id);
    }

    /**
     * 积分明细列表
     * @author xiaolin
     * @date   2016-10-19
     * @return json
     */
    public function score_record() {
        $page_no = an_request('page_no', 1);
        $store_id = an_request('store_id');
        $time_condition = an_request('time_condition');

        //加密参数
        $sign_param = array(
            'page_no' => $page_no,
            'store_id' => $store_id,
            'time_condition' => $time_condition
        );

        //参数验证
        $this->id_validation($store_id, '店铺ID不能为空');
        // 验证接口
        $this->api_stat_id = api_helper::check_sign($sign_param, 1);

        $store = store_helper::get_store($store_id);
        if ($this->user_id != $store['user_id'] && $this->user_id != $store['owner_id']) {
            api_helper::return_data(1, '你没有操作权限');
        }
        $filter['user_id'] = $store['user_id'];

        if ($time_condition) {
            switch ($time_condition) {
                case 1:
                    $filter['add_time >='] = datetime(strtotime('now -1 month'), 'Y-m-d 00:00:00');
                    $filter['add_time <='] = datetime();
                    break;
                case 2:
                    $filter['add_time >='] = datetime(strtotime('now -3 month'), 'Y-m-d 00:00:00');
                    $filter['add_time <='] = datetime();
                    break;
                case 3:
                    $filter['add_time >='] = datetime(strtotime('now -6 month'), 'Y-m-d 00:00:00');
                    $filter['add_time <='] = datetime();
                    break;
                case 4:
                    $filter['add_time <='] = datetime(strtotime('now -6 month'), 'Y-m-d 00:00:00');
                    break;
                default:
            }
        }

        $count = _model('score_record')->getTotal($filter);
        if (!$count) {
            api_helper::return_data(1, '暂无数据');
        }

        //分页配置
        $pager = new Pager($this->per_page);
        $pager->generate($count);
        $page_arr['page_no'] = $page_no;
        $page_arr['pages'] = $pager->getPages();

        $score_list = _model('score_record')->field('id', 'score', 'res_id', 'add_time')->getList($filter, 'ORDER BY `add_time` DESC ' . $pager->getLimit($page_no));

        if ($score_list) {
            foreach ($score_list as $k => $v) {
                $score_list[$k]['res_name'] = $this->get_record_type_name('score_type', $v['res_id']);
                $score_list[$k]['score'] = format_money($v['score']);
                unset($score_list[$k]['res_id']);
            }
        }

        $score = user_helper::get_score_by_id($store['user_id']); //获取积分
        api_helper::return_data(0, 'success', array(
            'score_list' => $score_list,
            'score' => format_money($score),
            'pages' => $page_arr
        ), $this->api_stat_id);
    }

    /**
     * 积分记录详情
     * @author xiaolin
     * @date   2016-10-19
     * @return json
     */
    public function score_record_detail() {
        $score_record_id = an_request('score_record_id');

        //参数验证
        $this->id_validation($score_record_id, '积分记录ID不能为空');

        //加密参数
        $sign_param = array(
            'score_record_id' => $score_record_id
        );

        // 验证接口
        $this->api_stat_id = api_helper::check_sign($sign_param, 1);

        $score_record = _model('score_record')->field('id', 'score', 'res_id', 'order_id', 'add_time')->read($score_record_id);
        if (!$score_record) {
            api_helper::return_data(1, '此积分记录不存在');
        }

        //其他数据获取
        $score_record['res_name'] = $this->get_record_type_name('score_type', $score_record['res_id']);
        $score_record['score'] = format_money($score_record['score']);
        $score_record['order_code'] = $score_record['order_id'] ? order_helper::order_id_encode($score_record['order_id']): '';
        unset($score_record['res_id']);

        api_helper::return_data(0, 'success', array('score_record' => $score_record), $this->api_stat_id);
    }

    /**
     * 根据抱抱币、余额、积分的ID获取类型名称
     * @author xiaolin
     * @date   2016-09-19
     * @param string $table_name 表名
     * @param int $res_id 类型ID
     * @return string
     */
    private function get_record_type_name($table_name, $res_id) {
        $type_info = _uri($table_name, $res_id);
        if ($type_info && $type_info['status'] == 1) {
            return $type_info['res_name'];
        } else {
            return '其他途径';
        }
    }

    /**
     * 根据操作人ID获取姓名
     * @author xiaolin
     * @date   2016-09-19
     * @param string $table_name 表名
     * @param int $res_id 类型ID
     * @return string
     */
    private function get_admin_name($admin_id) {
        if ($admin_id) {
            return user_helper::get_realname_by_id($admin_id);
        } else {
            return '系统';
        }
    }

    /**
     * 验证id是否为空
     * @author xiaolin
     * @date   2016-08-31
     * @param int $id 要验证的id
     * @param string $info 提示消息
     * @return json
     */
    private function id_validation($id, $info = 'ID不能为空') {
        if (!$id) {
            api_helper::return_data(1, $info);
        }
    }
}